﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml;
using Catsland.Core;
using System.ComponentModel;

namespace Catsland.CatsPlugin
{
	public class BackgroundController : CatComponent
	{
		private float m_zOffset = 0.0f;
        [CategoryAttribute("Basic")]
        public float ZOffset
        {
            get { return m_zOffset; }
            set { m_zOffset = value; }
        }

		private bool m_yAdjust = true;
        [CategoryAttribute("Basic")]
        public bool AdjustY
        {
            get { return m_yAdjust; }
            set { m_yAdjust = value; }
        }


		public BackgroundController(GameObject gameObject)
			:base(gameObject)
		{

		}

        public override void EditorUpdate(int timeLastFrame)
        {
            base.EditorUpdate(timeLastFrame);
            Update(timeLastFrame);
        }

		public override void Update(int timeLastFrame)
		{
			base.Update(timeLastFrame);

            QuadRender quadRender = (QuadRender)m_gameObject.GetComponent(typeof(QuadRender).Name);

            if (quadRender == null)
			{
				Console.WriteLine("Error! BackgroundController needs QuadRender Component.");
				return;
			}

            float quadWidth = quadRender.Size.X;
			float viewWidth = Mgr<Camera>.Singleton.m_viewSize.X;
			float cameraX = Mgr<Camera>.Singleton.m_position.X;

			float activeWidth = quadWidth - viewWidth;

			Vector2 sceneXBound = Mgr<Scene>.Singleton._XBound;

			float cameraPositionPercent = (cameraX - sceneXBound.X) / (sceneXBound.Y - sceneXBound.X);

            m_gameObject.Position = new Vector2(cameraX + activeWidth / 2.0f - cameraPositionPercent * activeWidth,
                m_gameObject.Position.Y);
			if (m_yAdjust)
			{
				m_gameObject.Height = (Mgr<Camera>.Singleton.m_position.Y + m_zOffset - m_gameObject.Position.Y) / Mgr<Scene>.Singleton._ySin;
			}
		}

		public override bool SaveToNode(XmlNode node, XmlDocument doc)
		{
			XmlElement backgroundController = doc.CreateElement("BackgroundController");
			node.AppendChild(backgroundController);

			backgroundController.SetAttribute("zOffset", "" + m_zOffset);
			backgroundController.SetAttribute("adjustY", "" + m_yAdjust);

			return true;
		}

        public override void ConfigureFromNode(XmlElement node, Scene scene, GameObject gameObject)
        {
            base.ConfigureFromNode(node, scene, gameObject);

            m_zOffset = float.Parse(node.GetAttribute("zOffset"));
            m_yAdjust = bool.Parse(node.GetAttribute("adjustY"));
        }

		public static BackgroundController LoadFromNode(XmlNode node, Scene scene, GameObject gameObject)
		{
			XmlElement backgroundController = (XmlElement)node;
			float zOffset = float.Parse(backgroundController.GetAttribute("zOffset"));
			bool adjustY = bool.Parse(backgroundController.GetAttribute("adjustY"));

			BackgroundController newBackgroundController = new BackgroundController(gameObject);
			newBackgroundController.m_zOffset = zOffset;
			newBackgroundController.m_yAdjust = adjustY;

			return newBackgroundController;
		}
	}
}
